package com.woniuxy.openfeign.interceptor;

import com.woniuxy.openfeign.util.WebUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * openFeign 拦截器
 */
@Component
public class OpenFeignInterceptor implements RequestInterceptor {

    /**
     * 拦截没有token的请求
     * @param requestTemplate 请求对象
     */
    @Override
    public void apply(RequestTemplate requestTemplate) {
        HttpServletRequest request = WebUtil.getRequest();
        if (request!=null){
            String token = request.getHeader("token");
            if (token!=null){
                requestTemplate.header("token",token);
            }
        }
    }

}
